<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="../style/editor.css" rel="stylesheet" type="text/css">
<script>
	var sLangDir=window.opener.oUtil.langDir;
	document.write("<scr"+"ipt src='../language/"+sLangDir+"/bookmark.js'></scr"+"ipt>");
</script>
<script>writeTitle()</script>
<script>
/*** COMMON ***/
function GetElement(oElement,sMatchTag)
    {
    while (oElement!=null&&oElement.tagName!=sMatchTag)
        {
        if(oElement.tagName=="BODY")return null;
        oElement=oElement.parentNode;
        }
    return oElement;
    }
    
/*** REALTIME + PREVIEW ***/
function doWindowFocus()
    {
    window.opener.oUtil.onSelectionChanged=new Function("realTime()");
    updateList();
    }
    
function bodyOnLoad()
    {   
    window.onfocus=doWindowFocus;   
    window.opener.oUtil.onSelectionChanged=new Function("realTime()");
    updateList();
    realTime();
    }
    
function updateList()
    {
    var oEditor=window.opener.oUtil.oEditor;
    
    var lstBookmark = document.getElementsByName("lstBookmark")[0];
    
    while(lstBookmark.options.length!=0) lstBookmark.options[0] = null;
    
    var aNode = oEditor.document.getElementsByTagName("A");
    for(var i=0;i<aNode.length;i++)
        {
        if (aNode[i].name != "") 
            {
            var op = document.createElement("OPTION");
            op.text=aNode[i].name;
            op.value=aNode[i].name;
            lstBookmark.options[lstBookmark.options.length] = op;
            }
        }
    }
    
function selectBookmark(sName)
    {
    var lstBookmark = document.getElementById("lstBookmark");
    var sel = lstBookmark.selectedIndex;
    var oEditor=window.opener.oUtil.oEditor;

    var aNode = oEditor.document.getElementsByName(sName);
    if (aNode.length > 0) {
        var range = oEditor.document.createRange();
        range.selectNode(aNode[0]);
        oSel = oEditor.getSelection();
        oSel.removeAllRanges();
        oSel.addRange(range);
    }
    realTime();
    window.opener.realTime(window.opener.oUtil.obj);
    window.opener.oUtil.obj.selectElement(0);
    window.focus();
    lstBookmark.focus();   
    lstBookmark.selectedIndex = sel;
    }
    
function realTime()
    {
    
    var btnInsert = document.getElementById("btnInsert");
    var btnApply = document.getElementById("btnApply");
    var btnOk = document.getElementById("btnOk");
    var inpName = document.getElementById("inpName");
    
    var oEditor=window.opener.oUtil.oEditor;
    
    var oSel=oEditor.getSelection();
    var oEl = window.opener.getSelectedElement(oSel);
    
    if (oEl.nodeName == "A")
        {
        
        var range =oEditor.document.createRange();
        range.selectNode(oEl);
        oSel.removeAllRanges();
        oSel.addRange(range);        
        
        btnInsert.style.display="none";
        btnApply.style.display="block";
        btnOk.style.display="block";
        
        if(oEl.NAME)inpName.value=oEl.NAME; 
        if(oEl.name)inpName.value=oEl.name; 
        }
    else
        {
        btnInsert.style.display="block";
        btnApply.style.display="none";
        btnOk.style.display="none";

        inpName.value="";   
        }           
    }

function doApply()
    {
    
    var inpName = document.getElementById("inpName");
    
    var oEditor=window.opener.oUtil.oEditor;
    
    window.opener.oUtil.obj.saveForUndo();

    var oSel = oEditor.getSelection();
    var range = oSel.getRangeAt(0);
    
    if(inpName.value!="") {
        var emptySel = false;
        if (range.toString() == "") {
            emptySel = true;
            var node = oEditor.document.createTextNode("#");
            range.insertNode(node);

            range = oEditor.document.createRange();
            range.setStart(node, 0);
            range.setEnd(node, "#".length);

            oSel = oEditor.getSelection();
            oSel.removeAllRanges();
            oSel.addRange(range);            
        }

        var isSelInMidText = (range.startContainer.nodeType==Node.TEXT_NODE) && (range.startOffset>0)
        
        oEditor.document.execCommand("CreateLink", false, "#");        
        
        oSel = oEditor.getSelection();
        range = oSel.getRangeAt(0);
        
        //get A element
        if (range.startContainer.nodeType == Node.TEXT_NODE) {
            
            var node = (emptySel || !isSelInMidText ? range.startContainer.parentNode : range.startContainer.nextSibling); //A node
            range = oEditor.document.createRange();
            range.selectNode(node);
            
            oSel = oEditor.getSelection();
            oSel.removeAllRanges();
            oSel.addRange(range);
            
        }        
        
        var oEl = range.startContainer.childNodes[range.startOffset];
        oEl.removeAttribute("HREF");
        oEl.setAttribute("NAME", inpName.value);
        
    } else {
        var oEl = range.startContainer.childNodes[range.startOffset];
        oEl.setAttribute("HREF", "dummy");
        oEditor.document.execCommand("unlink", false, null);//unlink
    }
    
    updateList();
    realTime();
    
    //~~~~~~~~ Focus stuff
    var oEditor=window.opener.oUtil.oEditor;
    oEditor.focus()
    
    window.opener.realTime(window.opener.oUtil.obj);
    
    if (inpName.value!="") 
        {
            window.opener.oUtil.obj.selectElement(0);
        } else {
            window.opener.oUtil.activeElement=null;
        }
    
    window.setTimeout("window.focus()", 10);
    }
    
</script>
</head>
<body onload="loadTxt();bodyOnLoad()" style="overflow:hidden;">

<table width=100% height=100% align=center cellpadding=0 cellspacing=0>
<tr>
<td valign=top style="padding:5;height:100%">

    <table width=100% border="0">
    <tr>
    <td width="100%">
    <select name="lstBookmark" id="lstBookmark" style="width:100%" size="5" onchange="selectBookmark(this.value)" class="inpSel">
    </select>
    <td>
    </tr>
    </table><br>
    
    <table width=100%>
    <tr>
        <td nowrap><span id="txtLang" name="txtLang">Name</span>:</td>
        <td style="width:100%;padding-right:5"><INPUT type="text" ID="inpName" NAME="inpName" style="width:100%" class="inpTxt"></td>
    </tr>
    </table>

</td>
</tr>
<tr>
<td class="dialogFooter" style="padding:6px;" align="right">
    <table cellpadding=1 cellspacing=0>
    <tr>
    <td>
    <input type=button name=btnCancel id=btnCancel value="cancel" onclick="self.close()" class="inpBtn" onmouseover="this.className='inpBtnOver';" onmouseout="this.className='inpBtnOut'">
    </td>
    <td>
    <input type=button name=btnInsert id=btnInsert value="insert" onclick="doApply()" class="inpBtn" onmouseover="this.className='inpBtnOver';" onmouseout="this.className='inpBtnOut'">
    </td>
    <td>
    <input type=button name=btnApply id=btnApply value="apply" style="display:none" onclick="doApply()" class="inpBtn" onmouseover="this.className='inpBtnOver';" onmouseout="this.className='inpBtnOut'">
    </td>
    <td>
    <input type=button name=btnOk id=btnOk value=" ok " style="display:none;" onclick="doApply();self.close()" class="inpBtn" onmouseover="this.className='inpBtnOver';" onmouseout="this.className='inpBtnOut'">
    </td>
    </tr>
    </table>
</td>
</tr>
</table>

</body>
</html>